package com.yc.net.flydove;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicInteger;

public class FlyDoveClient {
	
	Socket socket;
	String recvDir = "D:\\dev\\yc138\\0623\\"; // 接收文件的目录:   d:/recv/
	
	public static void main(String[] args) throws IOException {
		FlyDoveClient client = new FlyDoveClient();
		client.connect();
		System.out.println("-----------------------");
		client.recv();
	}
	
	public void connect()  throws IOException{
		InetAddress localhost = InetAddress.getLocalHost();
		String ip = localhost.getHostAddress();// IP 192.168.0.106
		System.out.println("ip = " + ip);
		int last = ip.lastIndexOf(".")+1;
		String prefix = ip.substring(0, last);  // 192.168.0.
		
			
		for (int i = 1; i < 256; i++) {
			ip = prefix + i;
			InetAddress addr = InetAddress.getByName(ip);
			String _ip = ip;
			Thread t = new Thread(()->{
				try {
					if(addr.isReachable(10)) {
						// Local variable ip defined in an enclosing
						// scope must be final or effectively final
						System.out.println("连接 ip = " + _ip);
						// ip 可達
						try {
							socket = new Socket(_ip, 9876);
							System.out.println("服务器连接成功! "+socket.getInetAddress());
							synchronized (FlyDoveClient.this) {
								FlyDoveClient.this.notify();
							}
						} catch (IOException e) {
							// continue;
						}
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			});
			t.start();
		}
		
		while(socket == null ) {
			synchronized (this) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}	
			}
		}
		
	}
	
	// 接收文件
	public void recv() throws IOException {
		InputStream in = socket.getInputStream();
		DataInputStream dis = new DataInputStream(in); 
		new Thread(()->{
			while(true) {
				try {
					String s = dis.readUTF();
					if(s.startsWith("dir:")) {
						System.out.println("接收目录:demo");
						String dirName = s.substring(4);
						recvDir(dirName);
					} else if(s.startsWith("file:")) {
						System.out.println("接收file:a.txt");
						String fileName = s.substring(5);
						// 获取文件大小
						long length = dis.readLong();
						recvFile(fileName, length);
						System.out.println("接收file:a.txt   OK");
					} 
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
	
	public void recvDir(String dirname) {
		File dir = new File(recvDir + dirname);
		if(dir.exists() == false) {
			dir.mkdirs();
		}
	}
	
	
	public void recvFile(String filename, long length) throws IOException {
		File filepath = new File(recvDir + filename);
		InputStream in = socket.getInputStream();
		byte[] bytes = new byte[1024];
		int count;
		try(FileOutputStream fos = new FileOutputStream(filepath)) {
			long readLength = 0;
			while((count = in.read(bytes))>-1) {
				fos.write(bytes, 0, count);
				readLength += count;
				if(readLength >= length) {
					return;
				}
			}
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
